package com.xucranger.ojapi.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xucranger.ojapi.enity.DTO.AtcoderQueryDTO;
import com.xucranger.ojapi.enity.DTO.CodeforcesQueryDTO;
import com.xucranger.ojapi.enity.DTO.RankDTO;
import com.xucranger.ojapi.enity.DTO.common.PageParamDTO;
import com.xucranger.ojapi.enity.DTO.common.PageResultDTO;
import com.xucranger.ojapi.enity.DTO.common.ResponseDTO;
import com.xucranger.ojapi.enity.VO.AtcoderVO;
import com.xucranger.ojapi.enity.VO.CareeVO;
import com.xucranger.ojapi.enity.VO.RankVO;
import com.xucranger.ojapi.enity.domain.Atcoder;
import com.xucranger.ojapi.service.AtcoderService;
import com.xucranger.ojapi.mapper.AtcoderMapper;
import com.xucranger.ojapi.utils.SmartBeanUtil;
import com.xucranger.ojapi.utils.SmartPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author xucranger
* @description 针对表【atcoder】的数据库操作Service实现
* @createDate 2022-08-16 09:16:07
*/
@Service
public class AtcoderServiceImpl extends ServiceImpl<AtcoderMapper, Atcoder>
implements AtcoderService{

    @Autowired
    AtcoderMapper atcoderMapper;
    //根据acid查询比赛
    @Override
    public ResponseDTO<PageResultDTO<AtcoderVO>> queryContestbyid(AtcoderQueryDTO queryDTO) {
        Page page = SmartPageUtil.convert2QueryPage(queryDTO);
        List<Atcoder> entities = atcoderMapper.queryContestByPage(page,queryDTO);
        List<AtcoderVO> dtoList = SmartBeanUtil.copyList(entities,AtcoderVO.class);
        page.setRecords(dtoList);
        PageResultDTO<AtcoderVO> pageResultDTO = SmartPageUtil.convert2PageResult(page);
        return ResponseDTO.succData(pageResultDTO);
    }

    @Override
    public ResponseDTO<PageResultDTO<AtcoderVO>> queryContestbycontestname(AtcoderQueryDTO queryDTO) {
        Page page = SmartPageUtil.convert2QueryPage(queryDTO);
        List<Atcoder> entities = atcoderMapper.queryContestByPage(page,queryDTO);
        List<AtcoderVO> dtoList = SmartBeanUtil.copyList(entities,AtcoderVO.class);
        page.setRecords(dtoList);
        PageResultDTO<AtcoderVO> pageResultDTO = SmartPageUtil.convert2PageResult(page);
        return ResponseDTO.succData(pageResultDTO);
    }

    @Override
    public ResponseDTO<PageResultDTO<AtcoderVO>> queryContestbycontestname(String contestname) {
        PageParamDTO pageParam = new PageParamDTO();
        pageParam.setPageSize(10);
        pageParam.setPageNum(1);

        AtcoderQueryDTO atcoderQueryDTO = new AtcoderQueryDTO();
        atcoderQueryDTO.setAccontestname(contestname);

        atcoderQueryDTO.setPageNum(pageParam.getPageNum());
        atcoderQueryDTO.setPageSize(pageParam.getPageSize());

        return queryContestbycontestname(atcoderQueryDTO);

    }

    @Override
    public ResponseDTO<CareeVO> queryCareeByAcid(String acid) {
        List<Atcoder> entities = atcoderMapper.queryCareeByAcid(acid);

        List<Integer> scores = new ArrayList<>();
        List<String> contestdates = new ArrayList<>();
        List<String> contestnames = new ArrayList<>();

        //处理积分为-的情况，不能直接转为int
        for (Atcoder item:entities) {

            contestdates.add(item.getAccontestdate());
            contestnames.add(item.getAccontestname());
            //如果不是-，直接转为int
            if(!item.getAcscore().equals("-")) {
                scores.add(Integer.valueOf(item.getAcscore()));
            }
            //如果是-，用0代替
            else
            {
                scores.add(0);
            }
        }

        CareeVO resultVO = new CareeVO(scores,contestdates,contestnames);
        return ResponseDTO.succData(resultVO);

    }

    @Override
    public ResponseDTO<PageResultDTO<RankVO>> rank(RankDTO rankDTO) {
        Page page = SmartPageUtil.convert2QueryPage(rankDTO);
        List<RankVO> entities = atcoderMapper.rank(page,rankDTO.getType());
        page.setRecords(entities);
        PageResultDTO<RankVO> pageResultDTO = SmartPageUtil.convert2PageResult(page);
        return ResponseDTO.succData(pageResultDTO);
    }

}
